package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.BuyInBean;
import com.lovo.project.bean.Data1Bean;
import com.lovo.project.bean.Data2Bean;
import com.lovo.project.bean.SaleInBean;
import com.lovo.project.service.SaleInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/salein")
public class SaleInController {

    @Autowired
    SaleInService saleInService;


    @RequestMapping("/querySaleInList")
    public Page<SaleInBean> querySaleIn(Integer userId, Integer pg, Integer pgsize, String clientName, String startTime, String endTime) {
        QueryWrapper<SaleInBean> queryWrapper = new QueryWrapper<>();
        if (userId > 0) {
            queryWrapper.eq("user_id", userId);
        }
        if (clientName != null && clientName != "") {
            queryWrapper.like("salein_client_name", clientName);
        }
        if (startTime != null && startTime!="") {
            queryWrapper.ge("salein_time", startTime);
        }
        if (endTime != null && endTime!="") {
            queryWrapper.le("salein_time", endTime);
        }
        Page<SaleInBean> page = new Page<>(pg, pgsize);
        Page<SaleInBean> pageInfo = saleInService.querySaleInList(page, queryWrapper);
        return pageInfo;
    }

    @RequestMapping("/querySaleIn")
    public SaleInBean querySaleIn(Integer saleInId) {
        SaleInBean saleInBean = saleInService.querySaleIn(saleInId);
        return saleInBean;
    }

    @RequestMapping("/queryYear")
    public List<String> queryYear() {
        List<String> list = saleInService.queryYear();
        return list;
    }

    @RequestMapping("/queryData")
    public List<Data1Bean> queryData(String year) {
        List<Data2Bean> data2BeanList = saleInService.queryData2BeanList(year);
        List<Data1Bean> data1BeanList = new ArrayList<>();
        for (Data2Bean data2 : data2BeanList) {
            int index = -1;
            for (Data1Bean data1 : data1BeanList) {
                if (data1.getName().equals(data2.getName())) {
                    data1.getDates()[data2.getMonths()-1]=data2.getTotalPrice();
                    index=0;
                    break;
                }
            }
            if (index == -1){
                Data1Bean data1Bean = new Data1Bean();
                data1Bean.setDates(new double[12]);
                data1Bean.setType("bar");
                data1Bean.setName(data2.getName());
                data1Bean.getDates()[data2.getMonths()-1]=data2.getTotalPrice();
                data1BeanList.add(data1Bean);
            }
        }
        return data1BeanList;
    }
}
